import storage from 'storejs'
import axios from './axios.js'
import API from './api.js'

//获取token
export async function getToken() {
  var res = await new Promise((resolve) => {
    axios.post(API.getToken)
      .then((res) => {
        var token = res.data.token;
        storage.set('token', token);
        resolve(token)
      });
  });
  return res;
}


/**
 *
 * @param phone  手机号
 * @returns {boolean}
 */
export function regPhone(phone) {
  let reg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
  if (reg.test($.trim(phone))) {
    return true;
  }
  return false;
}

//校验密码：只能输入6-20个字母、数字、下划线
export function regPasswd(s) {
  var reg = /^(\w){6,20}$/;
  if (!reg.exec(s)) return false
  return true
}

export function regEmail(email) {
  var reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
  if (reg.test(email)) {
    return true;
  }
  return false;
}
